package cn.kgc.controller;

import cn.kgc.pojo.AppCategory;
import cn.kgc.pojo.AppVersion;
import cn.kgc.pojo.BackendAppInfo;
import cn.kgc.pojo.DataDictionary;
import cn.kgc.service.BackendService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("backend")
public class BackendController {
    Logger log = Logger.getLogger(BackendController.class);

    @Resource
    private BackendService backendService;

    @RequestMapping("applist")
    public String BackendView(Model model){
        List<DataDictionary> appFlatformList = backendService.appFlatformList();
        model.addAttribute("flatFormList",appFlatformList);
        List<AppCategory> categoryLevel1List = backendService.categoryLevel1List();
        model.addAttribute("categoryLevel1List",categoryLevel1List);
        return "backend/applist";
    }

    @RequestMapping("level")
    @ResponseBody
    public String level(@RequestParam Integer pid){
        List<AppCategory> appCategoryList = backendService.categoryLevel2List(pid);
        log.debug(appCategoryList);
        return JSONObject.toJSONString(appCategoryList);
    }

    @RequestMapping("backendappinfolist")
    @ResponseBody
    public String backendAppInfoList(Integer flatformId,Integer level1,Integer level2,
                                     Integer level3,String softwareName,String currentPage,String pageSize){

        //Page本身也是个ArrayList，里面还包含了分页的各项参数。在调用分页查询时要先设置分页前提，比如，每页5行，查询第2页：
        PageHelper.startPage(Integer.valueOf(currentPage), Integer.valueOf(pageSize));

        BackendAppInfo backendAppInfo = new BackendAppInfo("%"+softwareName+"%",flatformId,level1,
                level2,level3);

        Page<BackendAppInfo> backendAppInfoList = backendService.selAppList(backendAppInfo);

        //丢给pageInfo处理（方便拿到其他值(总条数，页数，页码等等)，当然你也可以不传，看你自己吧）
        PageInfo<BackendAppInfo> pageInfo = new PageInfo<BackendAppInfo>(backendAppInfoList);


//        log.debug("第几页：" + pageInfo.getPageNum());
//        log.debug("每页显示的数据量：" + pageInfo.getPageSize());
//        log.debug("总页数：" + pageInfo.getPages());
//        log.debug("总条数：" + pageInfo.getTotal());

        return JSONObject.toJSONString(pageInfo);
    }

    @GetMapping("appcheck/{id}/{vid}")
    public String appCheck(@PathVariable Integer id,@PathVariable Integer vid, Model model){
        //log.debug(id+"========================"+versionId);

        BackendAppInfo backendAppInfo = backendService.selOneByIds(id);
        AppVersion appVersion = backendService.selVersion(vid);

        //log.debug("====="+backendAppInfo);
        model.addAttribute("appInfo",backendAppInfo);
        model.addAttribute("versionInfo",appVersion);
        return "backend/appcheck";
    }


    @PostMapping("updappstatus")
    public String updAppStatus(BackendAppInfo backendAppInfo){

        Boolean bo = backendService.updAppStatus(backendAppInfo.getId(), backendAppInfo.getStatus());

        if (bo==true){
            return "redirect:applist";
        }

        return "backend/appcheck";
    }

    @RequestMapping("ret")
    public String retApplist(){
        return "redirect:applist";
    }

}
